const express = require('express')
const qs = require('querystring')
//路由容器
const users = express.Router()
const axios = require('axios')
const formidable = require("formidable");
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const jwt = require("jsonwebtoken")//解析token

// var http = require('http')
const { log } = require('console')
users.get('/usersMenu', (req, res) => {
    res.render('admin/usersMenu',{
        userInfo: jwt.decode(req.session.token)
    })
})
// 查询所有用户
users.get('/ManagerUsers', (req, res) => {
    let {page ,size } = req.query;
    console.log("ManagerUsers");
    console.log(req.body);
    axios({
        url: 'consumer/ManagerUsers',
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})
// 通过id查询用户信息
users.get('/findById', (req, res) => {
    let {userId  } = req.query;
    console.log("ManagerUsers");
    console.log(req.body);
    axios({
        url: 'consumer/findById',
        method: 'GET',
        params: {
            userId: userId
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})

// 添加用户
users.post('/addAUser', (req, res) => {
    let {email ,password,sex,telephone,nickname,birth,flag} = req.query;
    console.log(birth);
    axios({
        url: 'consumer/addAUser',
        method: 'POST',
        params: {
            email: email,
            password: password,
            sex: sex,
            telephone: telephone,
            nickname: nickname,
            birth: birth,
            flag: flag
        }
    })
    .then(data => {
        res.send(data.data)
        // console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})
// 修改用户信息
users.post('/updateAVip', (req, res) => {
    let {email ,password,sex,telephone,nickname,birth,flag,userId} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/updateUser',
        method: 'PUT',
        params: {
            email: email,
            password:password ,
            sex: sex,
            telephone: telephone,
            nickname: nickname,
            birth: birth,
            flag: flag,
            userId:userId 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
        // console.log(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 上传头像
users.put('/updateAVipImg',multipartMiddleware,(req,res)=>{
        let file = req.files.multipartFile;
        let {userId} = req.query;
        console.log(userId);
        console.log(file.headers)
        const uri = file.path
        console.log(uri);
        let multipartFile = new FormData();
        multipartFile.append('image', fs.createReadStream(uri));
        axios.post(`consumer/updateAHeadPhoto?userId=${userId}`,
        multipartFile,
        {
            headers: multipartFile.getHeaders() 
        })
        .then((data) => {
        console.log("success============")
        res.send({ data: data.data })
        }).catch((err) => {
            console.log("err");
            res.send(err) 
        })
})

// 模糊查询
users.get('/selectUsers', (req, res) => {
    let {nickname,page,size} = req.query;
    console.log(nickname);
    console.log(page);
    console.log(size);
    nickname = qs.escape(nickname);
    // console.log(nickname);
    // console.log(qs.unescape(nickname));
    // nickname = "%E5%BC%A0";
    axios({
        url: `http://www.mianded.top:8081/consumer/selectUsers/${nickname}`,
        method: 'GET',
        params: {
            page:page,
            size:size,
        },
        headers:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
        // console.log("+++++++");
        // console.log(data.config)
    })
    .catch(err => {
        res.send(err)
        // console.log("--------");
        console.log(err);
    })
})
// 根据用户状态查询
users.get('/findUsersByClass', (req, res) => {
    let {flag,page,size} = req.query;
    console.log(page);
    console.log(size);
    axios({
        url: `http://www.mianded.top:8081/consumer/findUsersByClass/${flag}`,
        method: 'GET',
        params: {
            page:page,
            size:size,
        },
        headers:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
        // console.log("+++++++");
        // console.log(data.config)
    })
    .catch(err => {
        res.send(err)
        // console.log("--------");
        console.log(err);
    })
})
// 批量删除用户
users.delete('/deleteUsers', (req, res) => {
    let {ids } = req.query;
    console.log("++++++++");
    console.log(ids);
    axios({
        url: `/consumer/deleteUsers/${ids}`,
        method: 'DELETE',
    })
    .then(data => {
        res.send(data.data)
        console.log(data.config)
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})
// 删除单个用户
users.delete('/deleteAUser', (req, res) => {
    let {userId  } = req.query;
    console.log(userId );
    axios({
        url: `/consumer/deleteAUser/${userId}`,
        method: 'DELETE',
    })
    .then(data => {
        res.send(data.data)
        console.log(data.config)
        
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})




// 跳转至用户记录管理
users.get('/usersRecord', (req, res) => {
    res.render('admin/usersRecord',{
        userInfo: jwt.decode(req.session.token)
    })
})
// 查询所有用户记录
users.get('/findUserMessage', (req, res) => {
    let {page ,size } = req.query;
    console.log("findUserMessage");
    console.log(req.body);
    axios({
        url: 'consumer/findUserMessage',
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})
// 通过用户名查找用户记录
users.get('/findSuchMessage', (req, res) => {
    let {page ,size,nickname } = req.query;
    console.log("findSuchMessage");
    console.log(req.body);
    axios({
        url: 'consumer/findSuchMessage',
        method: 'GET',
        params: {
            page: page,
            size: size,
            nickname 
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})


// 跳转至单个用户详细浏览记录页面
users.get('/usersRecordBrowse', (req, res) => {
    let {userId} =req.query;
    res.render('admin/usersRecordBrowse',{
        info:userId,
        userInfo: jwt.decode(req.session.token)
    })
})
// 查看单个用户的商品浏览记录
users.get('/findByBrowse', (req, res) => {
    let {page,size,userId} =req.query;
    axios({
        url: 'consumer/findByBrowse',
        method: 'GET',
        params: {
            page: page,
            size: size,
            userId:userId
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
    })
    .catch(err => {
        console.log(err);
    })
})
// 模糊查询用户的浏览记录
users.get('/findSuchInBrowse', (req, res) => {
    let {goodsName ,page,size,userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/findSuchInBrowse`,
        method: 'GET',
        params: {
            page:page,
            size:size,
            goodsName:goodsName,
            userId: userId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})

// 跳转至单个用户详细浏览记录页面
users.get('/usersRecordOrder', (req, res) => {
    let {userId} =req.query;
    res.render('admin/usersRecordOrder',{
        info:userId,
        userInfo: jwt.decode(req.session.token)
    })
})
// 查看单个用户的构购买记录
users.get('/findByOrder', (req, res) => {
    let {page,size,userId} =req.query;
    axios({
        url: 'consumer/findByOrder',
        method: 'GET',
        params: {
            page: page,
            size: size,
            userId: userId
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data.data)
    })
    .catch(err => {
        console.log(err);
    })
})
// 模糊查询用户的购买记录
users.get('/findSuchInOrder', (req, res) => {
    let {orderNo  ,page,size,userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/findSuchInOrder`,
        method: 'GET',
        params: {
            page:page,
            size:size,
            orderNo :orderNo ,
            userId: userId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})
// 查看用户是否实名认证，
users.get('/findRealNameA', (req, res) => {
    let {userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/findRealName`,
        method: 'GET',
        params: {
            userId: userId
        }
    })
    .then(data => {
        console.log(data.data);
        if(data.status==500){
            req.session.error={
                status:data.status,
                msg:"很抱歉服务器出现故障，工作人员正在努力修复"
            }

        }else if(data.status==404){
            req.session.error={
                status:data.status,
                msg:"你当前访问的页面不存在"
            }
        }
        console.log(req.session.error);
        // console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log('-------------err');
        console.log(err);
        if(err.response.status==500){
            req.session.error={
                status:err.response.status,
                msg:"很抱歉服务器出现故障，工作人员正在努力修复"
            }
        }else if(err.response.status==404){
            req.session.error={
                status:err.response.status, 
                msg:"你当前访问的页面不存在"
            }
        }
        res.send(err)
    })
})
// 根据用户实名认证状态查询
users.get('/findAllRealName', (req, res) => {
    let {flag,page,size} = req.query;
    console.log(page);
    console.log(size);
    axios({
        url: `http://www.mianded.top:8081/consumer/findAllRealName`,
        method: 'GET',
        params: {
            page:page,
            size:size,
            flag:flag 
        },
        headers:{
            'Content-Type':'application/x-www-form-urlencoded'
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
        // console.log("+++++++");
        // console.log(data.config)
    })
    .catch(err => {
        res.send(err)
        console.log("--------");
        console.log(err);
    })
})

// 跳转至用户的实名认证申请页面
users.get('/findRealName', (req, res) => {
    let {userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/findRealName`,
        method: 'GET',
        params: {
            userId: userId
        }
    })
    .then(data => {
        console.log();
       res.render('admin/usersRealName',{
        info:data.data,
        userInfo: jwt.decode(req.session.token)
       })
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})
// 通过实名认证
users.get('/passARealName', (req, res) => {
    let {userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/passARealName`,
        method: 'GET',
        params: {
            userId: userId
        }
    })
    .then(data => {
        console.log();
        res.send(data.data)
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})
// 拒绝实名认证
users.get('/refuseARealName', (req, res) => {
    let {userId} =req.query;
   console.log(req.query);
    axios({
        url: `consumer/refuseARealName`,
        method: 'GET',
        params: {
            userId: userId
        }
    })
    .then(data => {
        console.log();
        res.send(data.data)
    })
    .catch(err => {
        res.send(err)
        console.log(err);
    })
})

module.exports=users;